package com.bonana.bozone.controller;
import com.bonana.core.entity.CoreUserProfile;
import com.bonana.core.entity.mybatis.Banner;
import com.bonana.core.entity.mybatis.Project;
import com.bonana.core.result.Response;
import com.bonana.core.result.ResponseCode;
import com.bonana.core.services.BannerService;
import com.bonana.core.services.BoBoService;
import com.bonana.core.services.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Administrator on 2016/7/5.
 */
@Controller

public class IndexController {

    @Autowired
    BoBoService boBoService;
    @Autowired
    ProjectService projectService;
    @Autowired
    BannerService bannerService;
    @Autowired
    private HttpSession session;

    @RequestMapping(value="/")
    public String Index(Model model) {
        String loginedUser = (String) session.getAttribute("session.username");
        if(loginedUser != null){
            model.addAttribute("loginUser", loginedUser);
        }

        List<CoreUserProfile> indexBobo = boBoService.getIndexBobos(9);
        List<Project> indexProject = projectService.getIndexProjs();
        List<Banner> topBanner = bannerService.getTopBanner();
        List<Banner> adBanner = bannerService.getAdBanner();

        model.addAttribute("indexBobo", indexBobo);
        model.addAttribute("indexProject", indexProject);
        model.addAttribute("topBanner", topBanner);
        model.addAttribute("adBanner", adBanner);
        return "index";
    }

    @RequestMapping("login")
    public String loginView() {
        return "login";
    }

    @RequestMapping("/logout")
    @ResponseBody
    public Response logoout(HttpServletRequest request) {
        session.removeAttribute("session.uid");
        session.removeAttribute("session.name");
        session.removeAttribute("session.username");
        return new Response(ResponseCode.SUCC);
    }
}
